package com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.entity.InformationList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemLvInformationAvtivityAdapter extends BaseAdapter {
    
    private List<InformationList.ListEntity> objects = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvInformationAvtivityAdapter(Context context, List<InformationList.ListEntity> list) {
        this.context = context;
        objects = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<InformationList.ListEntity> objects) {
        this.objects = objects;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public InformationList.ListEntity getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_information_avtivity, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((InformationList.ListEntity) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final InformationList.ListEntity object, ViewHolder holder) {
//        holder.imgvItemLvInformationActivity.setImageURI(Uri.parse(object));
        holder.imgvItemLvInformationActivity.setImageURI(Uri.parse(object.getImgUrl()));
        holder.tvItemLvInformationActivity.setText(object.getTitle());
        holder.tvContactItemLvInformationActivity.setText(object.getContent());
        holder.tvDataItemLvInformationActivity.setText(jiequ(object.getCreate_time()));
    }

    

    static class ViewHolder {
        @Bind(R.id.imgv_item_lv_information_activity)
        SimpleDraweeView imgvItemLvInformationActivity;
        @Bind(R.id.tv_item_lv_information_activity)
        TextView tvItemLvInformationActivity;
        @Bind(R.id.tv_data_item_lv_information_activity)
        TextView tvDataItemLvInformationActivity;
        @Bind(R.id.tv_contact_item_lv_information_activity)
        TextView tvContactItemLvInformationActivity;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    public String jiequ(String string){
        return string.substring(0,10);
    }
    
}
